Ext.define('App.kpiconfiguration.view.KpiConfigurationEditor', {
    extend: 'Extend.window.FormWindow',
    alias: ['widget.KpiConfigurationEditor'],
    requires: ['App.kpiconfiguration.field.KpiDataTypeCombobox', 'App.datasource.field.TableFieldGridField'],
    title: '指标编辑',
    config: {entity: 'entity'},
    width: 300,
    height: 200,
    defaults: {
        columnWidth: .3333,
        allowBlank: false,
        labelWidth: 80,
        blankText: '不能为空'
    },
    items: [{xtype: 'hidden', name: 'id'}, {
        xtype: 'textfield',
        name: 'field',
        fieldLabel: '指标名称',
        allowBlank: false,
        blankText: '指标名称为必填字段，不能为空！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>']
    }, {
        xtype: 'KpiDataTypeCombobox',
        name: 'dataType',
        fieldLabel: '数据类型',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>'],
        listeners: {
            change: function ($this, the, eOpts) {
                var nextdata = this.up().down('[name="format"]');
                if (this.value == 'date') {
                    nextdata.setDisabled(false);
                } else {
                    nextdata.setDisabled(true);
                    nextdata.setValue('');
                }
            }
        }
    }, {
        xtype: 'combobox',
        name: 'format',
        fieldLabel: '日期格式',
        allowBlank: true,
        disabled: true,
        queryModel: 'local',
        displayField: 'value',
        valueField: 'value',
        editable: false,
        store: {data: [{value: 'Y-m-d'}, {value: 'd-m-Y'}, {value: 'm-d-Y'}, {value: 'm-d'}, {value: 'Y/m/d'}, {value: 'd/m/Y'}, {value: 'm/d/Y'}, {value: 'm/d'}]}
    }, {
        xtype: 'numberfield',
        name: 'width',
        fieldLabel: '显示宽度(px)',
        allowBlank: true
    }, {
        xtype: 'textfield',
        name: 'formatter',
        fieldLabel: '格式化方式',
        allowBlank: true
    }, {
        xtype: 'combobox',
        name: 'judgmentStateMode',
        fieldLabel: '点灯类型',
        editable: false,
        valueField: 'value', displayField: 'name',
        store: {
            fields: ['value', 'name'],
            data: [{value: '', name: ''}, {value: '上限优先', name: '上限优先'}, {value: '下限优先', name: '下限优先'}, {value: '均值优先', name: '均值优先'}]
        },
        allowBlank: true,
        tpl: ['<ul class="x-list-plain"><tpl for=".">',
            '<li role="option" class="x-boundlist-item" style="height: 21px;">{name}</li>',
            '</tpl></ul>']
    }, {
        xtype: 'combobox',
        name: 'metricType',
        fieldLabel: '度量类型',
        editable: false,
        valueField: 'value', displayField: 'name',
        store: {
            fields: ['value', 'name'],
            data: [{value: '', name: ''}, {value: '范围', name: '范围'}, {value: '进度', name: '进度'}, {value: '质量', name: '质量'}, {value: '效率', name: '效率'}, {value: '其它', name: '其它'}]
        },
        allowBlank: true,
        tpl: ['<ul class="x-list-plain"><tpl for=".">',
            '<li role="option" class="x-boundlist-item" style="height: 21px;">{name}</li>',
            '</tpl></ul>']
    }, {
        xtype: 'booleancombobox',
        name: 'readOnly',
        fieldLabel: '只读',
        allowBlank: true
    }, {
        xtype: 'textarea',
        name: 'description',
        fieldLabel: '描述',
        columnWidth: 1,
        allowBlank: true
    }, {
        xtype: 'textarea',
        name: 'expression',
        fieldLabel: '计算表达式',
        allowBlank: true,
        columnWidth: 1,
        listeners: {
            selected: function (record) {
                var me = this;
                var data = record.data.fieldName;
                this.setValue(me.getValue() + data);
            }

        }

    }, {
        xtype: 'TableFieldGridField',
        name: 'tableFields',
        columnWidth: 1,
        height: 260,
        roweditable: false,
        columns: [{
            text: 'ID',
            dataIndex: 'id',
            hidden: true,
            sortable: false
        }, {
            text: '字段名称',
            dataIndex: 'alias',
            editor: {maxLength: 128},
            flex: 4
        }, {
            text: '字段',
            dataIndex: 'fieldName',
            editor: {allowBlank: false, blankText: "字段为必填字段，不能为空！", maxLength: 128},
            flex: 4
        }, {
            text: '数据类型',
            dataIndex: 'dataType',
            editor: {xtype: 'DataTypeComboBox', allowBlank: false, blankText: "数据类型为必填字段，不能为空！"},
            flex: 2
        }],
        addHandler: function () {
            var me = this;
            var ids = [];
            this.getStore().each(function (item) {
                ids.push(item.get('id'))
            });
            Ext.create('App.kpiconfiguration.view.KpiConfigurationSelectionWindow', {
                height: 520,
                width: 840,
                grid: 'App.datasource.view.TableFieldSelection',
                ids: ids,
                listeners: {
                    selected: function (record) {
                        if (record.length > 0) {
                            me.loadRecords(record, true);
                        }
                    }
                }
            }).show();
        },
        listeners: {
            rowdblclick: function ($this, record, element, rowIndex, e, eOpts) {
                this.up().down('[name="expression"]').fireEvent('selected', record);
            }
        }
    }]
});